package com.webinsky.folder.persist.dao;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.webinsky.common.proj.DelFlag;
import com.webinsky.common.utils.db.BasicSqlProvider;
import com.webinsky.common.utils.db.PageHelper;
import com.webinsky.common.utils.db.SQLBuilder;

public class FolderItemSqlProvider extends BasicSqlProvider {

	private String cols[] = new String[]{"id", "name", "sort", "del", "create_at", "update_at", "language", "site_id"};
	private String countCols[] = new String[]{"count(*)"};
	
	public FolderItemSqlProvider() {
		super("item");
	}
	
	@SuppressWarnings("unchecked")
	public String selectByIds(Map<String, Object> param){
		List<Integer> ids = (List<Integer>)param.get("ids");
		SQLBuilder sqlBuilder = getSelectBuilder(cols);
		sqlBuilder.WHERE("del = " + DelFlag.UNDel.getFlag());
		if ( ids != null && !ids.isEmpty() ){
			List<String> strIds = ids.stream().map(e -> e.toString()).collect(Collectors.toList());
			sqlBuilder.AND();
			sqlBuilder.WHERE("id in (" + String.join(",", strIds) + ")");
		}
		return sqlBuilder.toString();
	}
	
	public String selectByNameAndDel(Map<String, Object> param){
		String name = (String)param.get("name");
		String lang = (String)param.get("language");
		Integer del = (Integer)param.get("del");
		Integer pageSize = (Integer)param.get("pageSize");
		Integer pageStart = (Integer)param.get("pageStart");
		SQLBuilder sqlBuilder = getSelectBuilder(cols);
		if ( name != null ){
			sqlBuilder.WHERE(" name like '%" + name + "%'");
		}
		if ( lang != null ){
			sqlBuilder.WHERE(" language ='" + lang + "'");
		}
		if ( del != null ){
			sqlBuilder.WHERE(" del = " + del);
		}
		sqlBuilder.ORDER_BY("sort asc");
		if ( pageStart != null && pageSize != null && pageStart >=0 && pageSize > 0 ){
			return PageHelper.startPage(sqlBuilder.toString(), pageStart, pageSize);
		}
		return sqlBuilder.toString();
	}
	
	public String selectCountByNameAndDel(Map<String, Object> param){
		String name = (String)param.get("name");
		String lang = (String)param.get("language");
		Integer del = (Integer)param.get("del");
		SQLBuilder sqlBuilder = getSelectBuilder(countCols);
		if ( name != null ){
			sqlBuilder.WHERE(" name like '%" + name + "%'");
		}
		if ( lang != null ){
			sqlBuilder.WHERE(" language ='" + lang + "'");
		}
		if ( del != null ){
			sqlBuilder.WHERE(" del = " + del);
		}
		return sqlBuilder.toString();
	}
	
	public String selectByBookNameAndDel(Map<String, Object> param) {
		String name = (String)param.get("name");
		String lang = (String)param.get("language");
		Integer siteId = (Integer)param.get("siteId");
		Integer del = (Integer)param.get("del");
		Integer pageSize = (Integer)param.get("pageSize");
		Integer pageStart = (Integer)param.get("pageStart");
		SQLBuilder sqlBuilder = getSelectBuilder(cols);
		if ( name != null ){
			sqlBuilder.WHERE(" name like '%" + name + "%'");
		}
		if ( lang != null ){
			sqlBuilder.WHERE(" language ='" + lang + "'");
		}
		if ( siteId != null ) {
			sqlBuilder.WHERE(" site_id = " + siteId);
		}
		if ( del != null ){
			sqlBuilder.WHERE(" del = " + del);
		}
		sqlBuilder.ORDER_BY("sort asc");
		if ( pageStart != null && pageSize != null && pageStart >=0 && pageSize > 0 ){
			return PageHelper.startPage(sqlBuilder.toString(), pageStart, pageSize);
		}
		return sqlBuilder.toString();
	}
	
	public String selectCountByBookNameAndDel(Map<String, Object> param){
		String name = (String)param.get("name");
		String lang = (String)param.get("language");
		Integer siteId = (Integer)param.get("siteId");
		Integer del = (Integer)param.get("del");
		SQLBuilder sqlBuilder = getSelectBuilder(countCols);
		if ( name != null ){
			sqlBuilder.WHERE(" name like '%" + name + "%'");
		}
		if ( lang != null ){
			sqlBuilder.WHERE(" language ='" + lang + "'");
		}
		if ( siteId != null ) {
			sqlBuilder.WHERE(" site_id = " + siteId);
		}
		if ( del != null ){
			sqlBuilder.WHERE(" del = " + del);
		}
		return sqlBuilder.toString();
	}	

}
